package com.atguigu.spzx.order.controller;

import com.atguigu.spzx.model.dto.h5.OrderInfoDto;
import com.atguigu.spzx.model.entity.order.OrderInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.TradeVo;
import com.atguigu.spzx.order.service.OrderService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: OrderController
 * Package: com.atguigu.spzx.order.controller
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/17 17:25
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/order/orderInfo/auth")
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * 保存订单日志
     * @param orderId
     * @param processStatus
     */
    @GetMapping("saveOrderLog/{orderId}/{processStatus}")
    public void saveOrderLog(@PathVariable Long orderId,
                             @PathVariable Integer processStatus){
        orderService.saveOrderLog(orderId,processStatus);
    }


    /**
     *  更新订单状态
     * @param orderStatus
     */
    @GetMapping("updateOrderStatus/{orderStatus}")
    public void updateOrderStatus(@PathVariable Integer orderStatus){
        orderService.updateOrderStatus(orderStatus);
    }

    /**
     * 分页带条件(订单状态)查询订单列表
     * @param pageNum
     * @param pageSize
     * @param orderStatus
     * @return
     */
    @GetMapping("{pageNum}/{pageSize}")
    public Result<PageInfo<List<OrderInfo>>> findByPage(@PathVariable Integer pageNum,
                                              @PathVariable Integer pageSize,
                                              Integer orderStatus){
        PageInfo<OrderInfo> pageInfo = orderService.findByPage(pageNum,pageSize,orderStatus);
        return Result.build(pageInfo,ResultCodeEnum.SUCCESS);
    }
    /**
     * 立即购买 --> 订单确认页面 -> 用户的地址列表(发送到另一个服务的请求) ; 订单项信息-> 商品sku
     * @param skuId
     * @return
     */
    @RequestMapping("buy/{skuId}")
    public Result<TradeVo> buy(@PathVariable Long skuId){
        TradeVo tradeVo = orderService.buy(skuId);
        return Result.build(tradeVo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据订单id获取订单信息
     * @param orderId
     * @return
     */
    @GetMapping("{orderId}")
    public Result<OrderInfo> getById(@PathVariable Long orderId){
        OrderInfo orderInfo = orderService.getById(orderId);
        return Result.build(orderInfo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据订单编号查询订单信息
     * @param orderNo
     * @return
     */
    @GetMapping("getByOrderNo/{orderNo}")
    public OrderInfo getByOrderNo(@PathVariable String orderNo){
        OrderInfo orderInfo = orderService.getByOrderNo(orderNo);
        return orderInfo;
    }


    /**
     * /api/order/orderInfo/auth/submitOrder
     * 保存订单
     * @return
     */
    @PostMapping("submitOrder")
    public Result submitOrder(@RequestBody  OrderInfoDto orderInfoDto){
        Long orderId = orderService.submitOrder(orderInfoDto);
        return Result.build(orderId,ResultCodeEnum.SUCCESS);
    }
    /**
     * 获取订单项列表和总金额
     * @return
     */
    @GetMapping("trade")
    public Result<TradeVo> trade(){
        TradeVo tradeVo = orderService.trade();
        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }
}
